require "common"

local nSsqMapping = {{-1, 6},{-1, 6},{-1, 6},{-1, 5},{5, 4},{4, 3},{2, 1}};
--==================================复式计算=================================
function SsqCompute( nTotalRed, nMaxBingoRed, nTotalBlue, nMaxBingoBule)
	local nBingo = {0,0,0,0,0,0}

	--//防错处理
	if (nTotalRed < 6 or nTotalBlue < 1 or nTotalRed < nMaxBingoRed or nTotalBlue < nMaxBingoBule or nSsqMapping[nMaxBingoRed + 1][nMaxBingoBule + 1] == -1) then
		return nBingo
	end
	for i = 0, nMaxBingoRed  do
		for j = 0, nMaxBingoBule  do

			if (nSsqMapping[i + 1][j + 1] ~= -1) then
				if (j == 1) then
					nBingo[nSsqMapping[i + 1 ][j + 1]] =nBingo[nSsqMapping[i +1 ][j + 1]] + combination(nTotalRed - nMaxBingoRed, 6 - i) * combination(nMaxBingoRed, i)
				else
					nBingo[nSsqMapping[i + 1 ][j + 1]] =nBingo[nSsqMapping[i +1 ][j + 1]] + combination(nTotalRed - nMaxBingoRed, 6 - i) * combination(nMaxBingoRed, i) * (nTotalBlue - nMaxBingoBule)
				end
			end
		end
	end
	return nBingo--[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]
end
--======================胆拖计算===============================================
function SsqComputeDT(nTotalRedDT, nBingoRedDT, nTotalBlueDT, nBingoBuleDT,nTotalRed, nMaxBingoRed, nTotalBlue, nMaxBingoBule)
	local nBingo = {0,0,0,0,0,0}
	local donothing = 0
	if nTotalRedDT + nTotalRed < 6 or nTotalBlueDT + nTotalBlue < 1 or nTotalRedDT > 5 or nTotalBlueDT > 0 or nTotalRed < nMaxBingoRed
	or nTotalBlue < nMaxBingoBule or nTotalRedDT < nBingoRedDT or nTotalBlueDT < nBingoBuleDT then
		return nBingo
	end
	for i=0,nMaxBingoRed do
		for j=0,nMaxBingoBule do
			if i + nTotalRedDT > 6 or j + nTotalBlueDT > 1 or nSsqMapping[i + nBingoRedDT + 1][j + nBingoBuleDT + 1] == -1 then
				donothing = donothing + 1
			else
				if j == 1 then
					nBingo[nSsqMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nSsqMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 6 - nTotalRedDT - i) * combination(nMaxBingoRed, i) *  nMaxBingoBule
				else
					nBingo[nSsqMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nSsqMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 6 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * combination(nTotalBlue - nMaxBingoBule, 1)
				end

			end
		end
	end

	return nBingo--[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]
end



--返回注数,倍投数 , 投注串, 指令串
function random_ssq( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	--单式机选
	if butType == '1' then
		butNum = tonumber(itemList[1])
		if itemLen>2 or itemLen<1 or not butNum or butNum > 5 or butNum < 1 or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end
		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		for i=1,butNum do
			local nums = sel_dist_num(6,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33)
			local lq = math.random(1,16)
			table.sort(nums)
			zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])..format_num(nums[5])..format_num(nums[6]).."*"..format_num(lq)

			if i<butNum then zlStr = zlStr.."**" end
		end
		zlStr = zlStr.."#"..butPower
		butNum,butPower,butStr = check_ssq('1', zlStr)

	end

	return butNum, butPower, butStr, zlStr


end


--返回注数,倍投数,投注串
function check_ssq( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#", "0")
	qxStr = string.gsub( qjStr, "*", "0")

	--单式直选 1#012#红球*篮球**红球*篮球…#倍数 012 = 1,0,0,1
	if  butType == '1' then
		if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") or (itemList[2] and string.find(itemList[2],"%D")) then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end
		xxStr = string.gsub(itemList[1], "*", "x")
		xxItem = Split(xxStr, "xx")

		butNum = #xxItem

		if butNum > 5 or butNum < 1  then
			return 0
		end
		inStr = ""
		for i=1,butNum do
			local xItem = Split(xxItem[i], "x")
			if table.getn(xItem) ~=2 or string.len(xItem[1]) ~= 12 or string.len(xItem[2])~=2 then return 0 end

			local hStr = sort_str(xItem[1],33)
			local lStr = sort_str(xItem[2],16)
			if not hStr or not lStr then return 0 end

			butStr = butStr.."00"..format_num(butPower)..hStr.."~"..lStr.."^"
			if i == butNum then
				inStr = inStr..hStr.."*"..lStr.."#"..butPower
			else
				inStr = inStr..hStr.."*"..lStr.."**"
			end
		end


	--单式自选倍投 1#0122#红球*篮球#倍数 0122= 1,0,0,2
	elseif butType == '2' then
		butNum = 1
		if itemLen < 1 or itemLen > 2 or string.find(qxStr,"%D")  then
			return 0
		end

		local xItem = Split(itemList[1], "*")
		if table.getn(xItem) ~=2 or string.len(xItem[1]) ~= 12 or string.len(xItem[2])~=2 then return 0 end

		local hStr = sort_str(xItem[1],33)
		local lStr = sort_str(xItem[2],16)
		if not hStr or not lStr then return 0 end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		butStr = "00"..format_num( butPower )..hStr.."~"..lStr.."^"
		inStr = ""
		inStr = inStr..hStr.."*"..lStr.."#"..butPower

	--红球复式 1#013#红球号码*蓝球号码 013 = 1,0,0,3
	elseif butType == '3' then
		if itemLen < 1 or itemLen > 2 or string.find(qxStr,"%D")  then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		local xItem = Split(itemList[1], "*")
		if table.getn(xItem) ~=2 or string.len(xItem[1]) < 14 or string.len(xItem[1])>40 or string.len(xItem[2])~=2 then return 0  end

		local hStr = sort_str(xItem[1],33)
		local lStr = sort_str(xItem[2],16)
		if not hStr or not lStr then return 0 end

		butNum = combination((string.len(xItem[1])/2), 6)

		butStr = "10"..format_num( butPower )..hStr.."~"..lStr.."^"


	--蓝球复式 1#014#红球号码*蓝球号码 014 = 1,0,0,4
	elseif butType == '4' then
		if itemLen < 1 or itemLen > 2 or string.find(qxStr,"%D")  then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		local xItem = Split(itemList[1], "*")
		if table.getn(xItem) ~=2 or string.len(xItem[1]) ~= 12 or string.len(xItem[2]) <1 or string.len(xItem[2]) > 32 then return 0  end

		local hStr = sort_str(xItem[1],33)
		local lStr = sort_str(xItem[2],16)
		if not hStr or not lStr then return 0 end

		butNum = combination((string.len(xItem[2])/2), 1)

		butStr = "20"..format_num( butPower )..hStr.."~"..lStr.."^"


	--全复式 1#016#红球号码*蓝球号码 016 = 1,0,0,5
	elseif butType == '5' then
		if itemLen < 1 or itemLen > 2 or string.find(qxStr,"%D")  then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		local xItem = Split(itemList[1], "*")
		if table.getn(xItem) ~=2 or string.len(xItem[1]) < 12 or string.len(xItem[1])>40 or string.len(xItem[2]) <1 or string.len(xItem[2]) > 32 then return 0  end

		local hStr = sort_str(xItem[1],33)
		local lStr = sort_str(xItem[2],16)
		if not hStr or not lStr then return 0 end

		butNum = combination((string.len(xItem[1])/2), 6) * combination((string.len(xItem[2])/2), 1)

		if butNum < 2 then return 0 end

		butStr = "30"..format_num( butPower )..hStr.."~"..lStr.."^"


	--胆拖式 1#017#红球胆码*红球拖码*蓝球号码 017 = 1,0,0,6
	elseif butType == '6' then
		if itemLen < 1 or itemLen > 2 or string.find(qxStr,"%D")  then
			return 0
		end

		if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
		if butPower >99 or butPower < 1 then return 0 end

		local xItem = Split(itemList[1], "*")
		--if table.getn(xItem) ~=3 or string.len(xItem[1]) < 2 or string.len(xItem[1])>10 or string.len(xItem[2]) < 4 or string.len(xItem[2])>30 or string.len(xItem[1])+string.len(xItem[2]) < 12 or string.len(xItem[1])+string.len(xItem[2])>48 then return 0  end
	    if table.getn(xItem) ~=3 or string.len(xItem[1]) < 2  or string.len(xItem[1])>10 or string.len(xItem[2]) < 4 or string.len(xItem[2])>48 or string.len(xItem[1])+string.len(xItem[2]) <= 12  then return 0  end
		if string.len(xItem[3]) > 2 then
			if string.len(xItem[2]) > 32 then
				if string.len(xItem[3]) > 16 then
					return 0
				end
			else
				if string.len(xItem[3]) > 32 then
					return 0
				end
			end

			--if table.getn(xItem) ~=3 or string.len(xItem[1]) < 2 or string.len(xItem[1])>10 or string.len(xItem[2]) < 4 or string.len(xItem[2])>32 or string.len(xItem[1])+string.len(xItem[2]) <= 12  then return 0  end

			  --if string.len(xItem[1])+string.len(xItem[2]) <= 32 and (string.len(xItem[2]) < 4 or string.len(xItem[2])>32) then return 0 end
			  --if string.len(xItem[1])+string.len(xItem[2]) > 32 and (string.len(xItem[2]) < 4 or string.len(xItem[2])>16) then return 0 end
			if is_str_mix(xItem[1],xItem[2]) then return 0 end

			local hStr_d = sort_str(xItem[1],33)
			local hStr_t = sort_str(xItem[2],33)
			local lStr = sort_str(xItem[3],16)
			if not hStr_d or not hStr_t or not lStr then return 0 end

			butNum = combination((string.len(xItem[2])/2), (6-string.len(xItem[1])/2)) * (string.len(xItem[3])/2)

			butStr = "50"..format_num( butPower )..hStr_d.."*"..hStr_t.."~"..lStr.."^"
		elseif string.len(xItem[3]) == 2 then
			if is_str_mix(xItem[1],xItem[2]) then return 0 end

			local hStr_d = sort_str(xItem[1],33)
			local hStr_t = sort_str(xItem[2],33)
			local lStr = sort_str(xItem[3],16)
			if not hStr_d or not hStr_t or not lStr then return 0 end

			butNum = combination((string.len(xItem[2])/2), (6-string.len(xItem[1])/2)) * (string.len(xItem[3])/2)

			butStr = "40"..format_num( butPower )..hStr_d.."*"..hStr_t.."~"..lStr.."^"
		end

	else
		return 0
	end;
	return butNum,butPower,butStr,inStr
end
function query_ssq(butType,queryStr,djStr)
	local nBingo = {0,0,0,0,0,0}
	local temp = {0,0,0,0,0,0,0}
	local butPower = 1
	local itemList = {}
	local bullStr = {}
	local bingoNum = {}
	local qhStr = {}
	local nTotalRed = 0
	local nTotalBlue = 0
	local nTotalRedDT = 0
	local nTotalBlueDT = 0
	local nBingoRedDT = 0
	local nBingoBuleDT = 0
	itemList = Split( queryStr, "#" )
	xxStr = string.gsub(itemList[1], "*", "x")
	xxItem = Split(xxStr, "xx")


	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	bullStr = Split(djStr, "|")
	--单式自选
	if butType == '1' then
		nTotalRed = 6
		nTotalBlue = 1
		for i = 1,table.getn(xxItem) do
			qhStr = Split(xxItem[i],"x")

			for i = 1,2 do
				bingoNum[i] = ConputeBingoNum(qhStr[i],bullStr[i],2)
			end
			temp = nBingo
			nBingo = SsqCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])
			for j = 1, 6 do
				nBingo[j] = nBingo[j] + temp[j]
			end
		end
	--单式自选倍投
	elseif butType == '2' then
		nTotalRed = 6
		nTotalBlue = 1
		for i = 1,table.getn(xxItem) do
			qhStr = Split(xxItem[i],"x")

			for i = 1,2 do
				bingoNum[i] = ConputeBingoNum(qhStr[i],bullStr[i],2)
			end
			temp = nBingo
			nBingo = SsqCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])
			for j = 1, 6 do
				nBingo[j] = nBingo[j] + temp[j]
			end
		end
	--红球复式
	elseif butType == '3' then
		qhStr = Split(xxItem[1],"x")
		nTotalRed = string.len(qhStr[1])/2
		nTotalBlue = string.len(qhStr[2])/2
		for i = 1,2 do
			bingoNum[i] = ConputeBingoNum(qhStr[i],bullStr[i],2)
		end
		temp = nBingo
		nBingo = SsqCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])
		for j = 1, 6 do
			nBingo[j] = nBingo[j] + temp[j]
		end
	--篮球复式
	elseif butType == '4' then
		qhStr = Split(xxItem[1],"x")
		nTotalRed = string.len(qhStr[1])/2
		nTotalBlue = string.len(qhStr[2])/2
		for i = 1,2 do
			bingoNum[i] = ConputeBingoNum(qhStr[i],bullStr[i],2)
		end
		temp = nBingo
		nBingo = SsqCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])
		for j = 1, 6 do
			nBingo[j] = nBingo[j] + temp[j]
		end
	--全复式
	elseif butType == '5' then
		qhStr = Split(xxItem[1],"x")
		nTotalRed = string.len(qhStr[1])/2
		nTotalBlue = string.len(qhStr[2])/2
		for i = 1,2 do
			bingoNum[i] = ConputeBingoNum(qhStr[i],bullStr[i],2)
		end
		temp = nBingo
		nBingo = SsqCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])
		for j = 1, 6 do
			nBingo[j] = nBingo[j] + temp[j]
		end
	elseif butType == '6' then
		xItem = Split(xxItem[1], "x")
		nTotalRedDT = string.len(xItem[1]) / 2   --红球胆码
		nTotalBlueDT = 0	--篮球胆码
		nTotalRed = string.len(xItem[2]) / 2   --红球拖码
		nTotalBlue = string.len(xItem[3]) / 2	--篮球拖码
		bingoNum[1] = ConputeBingoNum(xItem[1],bullStr[1],2)    --红球胆码
		bingoNum[2] = ConputeBingoNum(xItem[2],bullStr[1],2) 	--红球拖码
		bingoNum[3] = 0											--篮球胆码
		bingoNum[4] = ConputeBingoNum(xItem[3],bullStr[2],2)	--篮球拖码
		nBingo = SsqComputeDT(nTotalRedDT,bingoNum[1],nTotalBlueDT,bingoNum[3],nTotalRed,bingoNum[2],nTotalBlue,bingoNum[4])
	end
	for i=1, 6 do
		nBingo[i] = nBingo[i] * butPower
	end
	return nBingo[1]..","..nBingo[2]..","..nBingo[3]..","..nBingo[4]..","..nBingo[5]..","..nBingo[6]
end
--test
--print(check_ssq('6',"15222533*0313*06#1"))
--print(check_ssq('2',"010203040905*01#12"))
--print(check_ssq('3',"0102030405060708091011121314151617181920*01"))
--print(check_ssq('4',"0102030433*0109"))
--print(check_ssq('5',"0102030405060708091011121314151617181920*010916#89"))
--print(check_ssq('6',"0102030405*060708091011121314151617181920212223242526272829*01"))
--print(check_ssq('1',"010203040905*01**010203040905*01**010203040905*01#12"))

--for i=1,1000 do
--	print(random_ssq('1',"5#11"))
--end
--print(SsqCompute(6,5,1,1))
--print(query_ssq('1',"101824272832*08#1","010205132229|08"))
--print(query_ssq('5',"010203040506070809*010203#1","010203040510|01"))
--print(query_ssq('6',"0102030405*07*07#10","010203040506|07"))
--print(SsqComputeDT(5,5,0,0,2,1,2,1))
